let user = { username: "admin1", password: "12345" };
const fs = require("fs");

let result = fs.readFileSync("persons.json", "utf8"); // 读取文件内容
let arr = JSON.parse(result);  // 将js字符串转成 json对象
let a = arr.find((item) => item.username === user.username); 

if (a) {
  console.log("用户名已存在");
} else {
  console.log("注册成功");
  arr.push(user);
  let str = JSON.stringify(arr); // 将对象转为 json字符串
  fs.writeFileSync("persons.json", str, { flag: "w" });
}
